/*
 * Copyright 2005-2013 sxhuayuan.com. All rights reserved.
 */
package com.sxhuayuan.parking.controller.admin;

import javax.annotation.Resource;

import com.sxhuayuan.parking.compenent.Filter;
import com.sxhuayuan.parking.controller.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.sxhuayuan.parking.compenent.Message;
import com.sxhuayuan.parking.compenent.Page;
import com.sxhuayuan.parking.compenent.Pageable;
import com.sxhuayuan.parking.compenent.web.RespMessage;
import com.sxhuayuan.parking.entity.Role;
import com.sxhuayuan.parking.service.RoleService;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller - 角色
 *
 * @author gaoyan
 * @version 1.0
 */
@RestController("adminRoleController")
@RequestMapping("/admin/role")
public class RoleController extends BaseController {

    @Resource(name = "roleServiceImpl")
    private RoleService roleService;

    /**
     * 编辑
     */
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public RespMessage get(@PathVariable Long id) {
        return RespMessage.builder().success().setDatas( roleService.findOne( id ) ).build();
    }

    /**
     * 保存
     * 修改：不能保存数据库已有的角色名
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public RespMessage save(@RequestBody @Validated Role role) {

        List<Filter> filters = new ArrayList<>();
        Filter filter = Filter.eq( "name", role.getName() );
        filters.add( filter );
        List<Role> all = roleService.findList( 1000, filters, null );

        if (all.size() > 0) {
            return RespMessage.error( "角色名已存在" );
        }

        role.setIsSystem( false );
        role.setAdmins( null );
        role.setAgents( null );
        roleService.save( role );
        return RespMessage.success();
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public RespMessage update(@RequestBody @Validated Role role) {
        Role pRole = roleService.findOne( role.getId() );
        if (pRole == null || pRole.getIsSystem()) {
            return RespMessage.error( Message.COMMON_UPDATE_FAIL );
        }

        List<Filter> filters = new ArrayList<>();
        Filter filter = Filter.eq( "name", role.getName() );
        Filter filter2 = Filter.ne( "name", pRole.getName() );
        filters.add( filter );
        filters.add( filter2 );
        List<Role> all = roleService.findList( 1000, filters, null );

        if (all.size() > 0) {
            return RespMessage.error( "更新失败，角色名已存在" );
        }

        roleService.update( role, "admins" );
        return RespMessage.success();
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public RespMessage delete(@RequestBody Long[] ids) {
        if (ids != null) {
            for (Long id : ids) {
                Role role = roleService.findOne( id );
                if (role != null && (role.getIsSystem() || (role.getAdmins() != null && !role.getAdmins().isEmpty()) || (role.getAgents() != null && !role.getAgents().isEmpty()))) {
                    return RespMessage.error( Message.COMMON_DELETE_NOT_ALLOW, role.getName() );
                }
            }
            roleService.delete( ids );
        }
        return RespMessage.success();
    }


    /**
     * 获取全部角色列表(超级管理员除外)，用于渲染下拉列表
     *
     * @param type 用户类型
     * @return RespMessage
     * @author helong
     * @data 2019/3/19
     */
    @GetMapping("/getlist")
    public RespMessage getList(String type) {
        List<Filter> filters = new ArrayList<>();
        filters.add( Filter.ne( "isSystem", true ) );
        if ("admin".equals(type)) {
            filters.add( Filter.eq( "type", Role.Type.admin ) );
        }
        if ("agent".equals(type)) {
            filters.add( Filter.eq( "type", Role.Type.agent ) );
        }
        if ("parkinglot".equals(type)) {
            filters.add( Filter.eq( "type", Role.Type.parkingLot ) );
        }
        List<Role> all = roleService.findList( 1000, filters, null );
        return RespMessage.builder().success().setDatas( all ).build();
    }


    /**
     * @param pageable 分页查询条件
     * @return RespMessage
     * @author helong
     * @data 2019/3/20
     */
    @PostMapping("/pagelist")
    public RespMessage pageList(@RequestBody Pageable pageable) {

        Page<Role> page = roleService.findPage( pageable );

        return RespMessage.builder().success().setDatas( page ).build();
    }
}